package com.example.thirdannotation.y2024;

import com.example.thirdannotation.y2024.bean.Person;
import com.example.thirdannotation.y2024.config.MyConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;

public class MyConfigTest {
    public static void main(String[] args) {
        // 获取bean
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
        System.out.println("ioc 容器加载完成....");
//        Person person =  annotationConfigApplicationContext.getBean(Person.class);
        Person person = (Person) annotationConfigApplicationContext.getBean("person");
        System.out.println("person-->"+person);

        // 获取注解的id
        String[] names = annotationConfigApplicationContext.getBeanNamesForType(Person.class);
        for (String name : names) {
            System.out.println("name-->"+name);
        }

        Map<String, Person> beansOfType = annotationConfigApplicationContext.getBeansOfType(Person.class);
        System.out.println("beansOfType-->"+beansOfType);

        String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println("beanDefinitionName-->"+beanDefinitionName);
            if (beanDefinitionName.equals("colorFactoryBean")) {
                Object bean = annotationConfigApplicationContext.getBean(beanDefinitionName);
                System.out.println("bean的实际类型-->"+bean.getClass());
                // 想要获取factoryBean本身，需要通过添加 & 前缀的方式
                Object factoryBean = annotationConfigApplicationContext.getBean("&"+beanDefinitionName);
                System.out.println("factoryBean-->"+factoryBean);
            }
        }
    }
}
